package ta.utils;

import java.util.List;

import javax.servlet.http.HttpServlet;

public class ServletGenerator
{
	static String rt = "\r\t";
	// 创建Dao类
	public static void createServlet(List<String> tablelist)
	{
		for (String tablename : tablelist)
		{
			String className=toFirstUpperCase(tablename);
			List<String[]> colsList = TableMeta.getColumnDatas(tablename);
			StringBuilder sb = new StringBuilder();
			sb.append("package " + CodeConfig.ServletPackageName + ";\n");
			sb.append("import java.io.IOException;\r\n"
					+ "import java.lang.reflect.InvocationTargetException;\r\n"
					+ "import java.util.Map;\r\n"
					+ "import javax.servlet.ServletException;\r\n"
					+ "import javax.servlet.annotation.WebServlet;\r\n"	
					+"import javax.servlet.http.HttpServlet;\r\n"
					+ "import javax.servlet.http.HttpServletRequest;\r\n"
					+ "import javax.servlet.http.HttpServletResponse;\r\n"
					+ "import org.apache.commons.beanutils.BeanUtils;\r\n"
					+ "\r\n"
					+ "import " +CodeConfig.DaoPackageName+"."+className+"Dao;\r\n"
					+ "import " +CodeConfig.BeanPackageName+"."+className+";\r\n"
					+ "import yzh.tools.PageInfo;\r\n");
			sb.append("\r\n"
					+ "@WebServlet(\"/admin/"+className+"Servlet\")\r\n");
			// 创建类，类名为表名首字母大写
			sb.append("public class " + className + "Servlet  extends HttpServlet{" + rt);
			sb.append("private static final long serialVersionUID = 1L;"+rt);
			//doPost
			sb.append(rt+"protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException\r\n"
					+ "	{\r\n"
					+ "		doGet(request, response);\r\n"
					+ "	}\r\n"+rt);
			//doGet
			sb.append(rt+"protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException\r\n"
					+ "	{\r\n"
					+ "		String type = request.getParameter(\"type\");\r\n"
					+ "		switch (type)\r\n"
					+ "		{\r\n"
					+ "		case \"getlist\":\r\n"
					+ "			getlist(request, response);\r\n"
					+ "			break;\r\n"
					+ "		case \"getbyid\":\r\n"
					+ "			getbyid(request, response);\r\n"
					+ "			break;\r\n"
					+ "		case \"add\":\r\n"
					+ "			add(request, response);\r\n"
					+ "			break;\r\n"
					+ "		case \"del\":\r\n"
					+ "			del(request, response);\r\n"
					+ "			break;\r\n"
					+ "		case \"edit\":\r\n"
					+ "			edit(request, response);\r\n"
					+ "			break;\r\n"
					+ "		default:\r\n"
					+ "			break;\r\n"
					+ "		}\r\n"
					+ "	}" + rt);
			//getall
			sb.append(rt+"protected void getlist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException\r\n"
					+ "	{\r\n"
					
					+"\t	String keyword = request.getParameter(\"keyword\");// null--\"\"\r\n"
					+ "		String currentpage = request.getParameter(\"page\");		"
					+ rt+"\tPageInfo<"+className+"> pb = new PageInfo<"+className+">();\r\n"
					+"\t	int page1 = currentpage == null ? 1 : Integer.valueOf(currentpage);\r\n"
					+ "		keyword = keyword == null ? \"\" : keyword;		\r\n"					
					+ "		pb.setCurrentpage(page1);\r\n"
					+ "		"+className+"Dao dao = new "+className+"Dao();\r\n"
					+ "\t\tdao.getAll(keyword,pb);		\r\n"
					+ "		request.setAttribute(\"pb\", pb);\r\n"
					+"\t\trequest.setAttribute(\"keyword\", keyword);"
					+ rt+"\trequest.getRequestDispatcher(\""+tablename+"/list.jsp\").forward(request, response);\r\n"
					+ "	}"+rt);
			//getbyid
			sb.append(rt+"protected void getbyid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException\r\n"
					+ "	{\r\n"
					+ "		String "+colsList.get(0)[0]+" = request.getParameter(\""+colsList.get(0)[0]+"\");\r\n"
					+ "		"+className+"Dao dao = new "+className+"Dao();\r\n"
					+ "		"+className+" "+tablename+" = dao.getById("+colsList.get(0)[0]+");\r\n"
					+ "		request.setAttribute(\""+tablename+"\", "+tablename+");\r\n"
					+ "		request.getRequestDispatcher(\""+tablename+"/edit.jsp\").forward(request, response);\r\n"
					+ "	}");
			//add
			sb.append("\r\n"
					+ "	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException\r\n"
					+ "	{\r\n"
					+ "		"+className+" "+tablename+" = new "+className+"();\r\n"
					+ "		Map<String, String[]> map = request.getParameterMap();\r\n"
					+ "		try\r\n"
					+ "		{\r\n"
					+ "			BeanUtils.populate("+tablename+", map);\r\n"
					+ "		}\r\n"
					+ "		catch (IllegalAccessException | InvocationTargetException e)\r\n"
					+ "		{\r\n"
					+ "			e.printStackTrace();\r\n"
					+ "		}\r\n"
					+ "		"+className+"Dao dao = new "+className+"Dao();\r\n"
					+ "		dao.add("+tablename+");\r\n"
					+ "		response.sendRedirect(\""+className+"Servlet?type=getlist&page=1\");\r\n"
					+ "	}"+rt);
			//del
			sb.append("\r\n"
					+ "	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException\r\n"
					+ "	{\r\n"
					+ "		String "+colsList.get(0)[0]+" = request.getParameter(\""+colsList.get(0)[0]+"\");\r\n"
					+ "		"+className+"Dao dao = new "+className+"Dao();\r\n"
					+ "		dao.del("+colsList.get(0)[0]+");\r\n"
					+ "		response.sendRedirect(\""+className+"Servlet?type=getlist&page=1\");\r\n"
					+ "	}"+rt);
			//modify
			sb.append("\r\n"
					+ "	protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException\r\n"
					+ "	{\r\n"
					+ "		"+className+" "+tablename+" = new "+className+"();\r\n"
					+ "		Map<String, String[]> map = request.getParameterMap();\r\n"
					+ "		try\r\n"
					+ "		{\r\n"
					+ "			BeanUtils.populate("+tablename+", map);\r\n"
					+ "		}\r\n"
					+ "		catch (IllegalAccessException | InvocationTargetException e)\r\n"
					+ "		{\r\n"
					+ "			e.printStackTrace();\r\n"
					+ "		}\r\n"
					+ "		"+className+"Dao dao = new "+className+"Dao();\r\n"
					+ "		dao.edit("+tablename+");\r\n"
					+ "		response.sendRedirect(\""+className+"Servlet?type=getlist&page=1\");\r\n"
					+ "	}"+rt);
		sb.append("\n}" + rt);
			// 创建文件
			FileHelper.genModuleTpl(
					CodeConfig.getRoot(CodeConfig.servletPath + "\\" + className + "Servlet.java"),
					sb.toString());
			System.out.println(CodeConfig.ServletPackageName + ": " + tablename + " 创建成功!");

		}
	}

	/**
	 * 首字母大写
	 */
	public static String toFirstUpperCase(String s)
	{
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}
	
	public static void main(String[] args)
	{
		List<String> list=TableMeta.getTables();		
		createServlet(list);
	}
}
